package com.zt.controller;

import com.zt.pojo.Checkgroup;
import com.zt.pojo.vo.CheckgroupVo;
import com.zt.service.CheckgroupService;
import com.zt.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/checkgroup")
public class CheckgroupController {
    @Autowired
    private CheckgroupService checkgroupService;

    /**
     * 分页模糊查询
     * @param checkgroupVo
     * @return
     */
    @RequestMapping("/findPage")
    public Result findPage(@RequestBody CheckgroupVo checkgroupVo){
        return checkgroupService.findPage(checkgroupVo);
    }

    /**
     * 添加检查组
     * @param checkgroup
     * @return
     */
    @PostMapping("/add")
    public Result add(Integer[] checkitemIds,@RequestBody Checkgroup checkgroup){
        try {
            checkgroupService.add(checkitemIds, checkgroup);
            return Result.success("添加成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.error("添加失败");
    }

    /**
     * 删除检查组
     * @param id
     * @return
     */
    @RequestMapping("/delete")
    public Result delete(Integer id){
        try {
            checkgroupService.delete(id);
            return Result.success("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.error("删除失败");
    }

    /**
     * 根据id查询检查组
     * @param id
     * @return
     */
    @RequestMapping("/findById")
    public Result findById( Integer id ) {
        try {
            Checkgroup checkGroup = checkgroupService.findById( id );
            return Result.success("查询成功",checkGroup);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.error("查询失败");
    }

    /**
     * 修改检查组
     * @param checkitemIds
     * @param checkgroup
     * @return
     */
    @RequestMapping("/edit")
    public Result update( Integer[] checkitemIds,@RequestBody Checkgroup checkgroup){
        try {
            checkgroupService.edit(checkitemIds,checkgroup);
            return Result.success("修改成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.error("修改失败");
    }

    /**
     * 回显检查项
     * @param id
     * @return
     */
    @RequestMapping("/findCheckItemIdsByCheckGroupId")
    public Result findCheckItemIdsByCheckGroupId(Integer id){
        try {
            Integer[] checkItemIdsByCheckGroupId = checkgroupService.findCheckItemIdsByCheckGroupId(id);
            return Result.success("查询成功",checkItemIdsByCheckGroupId);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.error("查询失败");
    }
    @RequestMapping("/findAll")
    public Result findAll(){
        return checkgroupService.findAll();
    }
}
